package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.macro.mall.dto.e.DataAnalysisPvEntDTO;
import com.macro.mall.dto.o.DataAnalysisPvOutDTO;
import com.macro.mall.mapper.*;
import com.macro.mall.model.*;
import com.macro.mall.service.DataAnalysisPvService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DataAnalysisPvServiceImpl implements DataAnalysisPvService {
    @Autowired
    private DpsVisitRiseMapper dpsVisitRiseMapper;
    @Autowired
    private BasicKeyTextMapper basicKeyTextMapper;
    @Autowired
    private BasicTopicMapper basicTopicMapper;
    @Autowired
    private BasicKeyImgMapper basicKeyImgMapper;

    @Override
    public PageInfo<DataAnalysisPvOutDTO> list(DataAnalysisPvEntDTO dataAnalysisPvEntDTO) {
        PageHelper.startPage(dataAnalysisPvEntDTO.getPage(),dataAnalysisPvEntDTO.getPageSize());
        DpsVisitRise dpsVisitRise = new DpsVisitRise();

        Date[] createTime = dataAnalysisPvEntDTO.getCreateTime();
        if (createTime != null){
            if (createTime[0] != null){
                dpsVisitRise.setBeginTime(createTime[0]);
            }
            if (createTime[1] != null){
                dpsVisitRise.setEndTime(createTime[1]);
            }
        }
        List<DpsVisitRise> list = dpsVisitRiseMapper.list(dpsVisitRise);
        PageInfo<DpsVisitRise> dpsVisitRisePageInfo = new PageInfo<>(list);
        List<DataAnalysisPvOutDTO> list1=new ArrayList<>();
        for (DpsVisitRise dpsVisitRise1:list){
            DataAnalysisPvOutDTO dataAnalysisPvOutDTO = new DataAnalysisPvOutDTO();
            BasicKeyText basicKeyText = basicKeyTextMapper.selectByPrimaryKey(dpsVisitRise1.getContentId());
            BasicTopic basicTopic = basicTopicMapper.selectByPrimaryKey(dpsVisitRise1.getContentId());
            BasicKeyImg basicKeyImg = basicKeyImgMapper.selectByPrimaryKey(dpsVisitRise1.getContentId());
            if (basicKeyText !=null){
                dataAnalysisPvOutDTO.setContentName(basicKeyText.getKeyText());
            }
            if (basicTopic !=null){
                dataAnalysisPvOutDTO.setContentName(basicTopic.getTopicName());
            }
            if (basicKeyImg !=null){
                dataAnalysisPvOutDTO.setContentName(basicKeyImg.getImgName());
                dataAnalysisPvOutDTO.setImgPath(basicKeyImg.getImgPath());
            }
            dataAnalysisPvOutDTO.setId(dpsVisitRise1.getId());
            dataAnalysisPvOutDTO.setCreatedDt(dpsVisitRise1.getCreatedDt());
            dataAnalysisPvOutDTO.setStatus(dpsVisitRise1.getStatus());
            dataAnalysisPvOutDTO.setHits(dpsVisitRise1.getHits());
            list1.add(dataAnalysisPvOutDTO);
        }
        PageInfo<DataAnalysisPvOutDTO> dataAnalysisPvOutDTOPageInfo = new PageInfo<>(list1);
        dataAnalysisPvOutDTOPageInfo.setTotal(dpsVisitRisePageInfo.getTotal());
        return dataAnalysisPvOutDTOPageInfo;
    }

    @Override
    public int modify(DataAnalysisPvEntDTO dataAnalysisPvEntDTO) {
        DpsVisitRise dpsVisitRise1 = dpsVisitRiseMapper.selectByPrimaryKey(dataAnalysisPvEntDTO.getId());
        BasicKeyText basicKeyText = basicKeyTextMapper.selectByPrimaryKey(dpsVisitRise1.getContentId());
        BasicTopic basicTopic = basicTopicMapper.selectByPrimaryKey(dpsVisitRise1.getContentId());
        BasicKeyImg basicKeyImg = basicKeyImgMapper.selectByPrimaryKey(dpsVisitRise1.getContentId());
        if (basicKeyText !=null){
            basicKeyText.setKeyText(dataAnalysisPvEntDTO.getKeyName());
            basicKeyTextMapper.updateByPrimaryKeySelective(basicKeyText);
        }
        if (basicTopic !=null){
            basicTopic.setTopicName(dataAnalysisPvEntDTO.getKeyName());
            basicTopicMapper.updateByPrimaryKeySelective(basicTopic);
        }
        if (basicKeyImg !=null){
            basicKeyImg.setImgName(dataAnalysisPvEntDTO.getKeyName());
            basicKeyImgMapper.updateByPrimaryKeySelective(basicKeyImg);
        }

        DpsVisitRise dpsVisitRise = new DpsVisitRise();
        dpsVisitRise.setId(dataAnalysisPvEntDTO.getId());
        dpsVisitRise.setHits(dataAnalysisPvEntDTO.getHits());
        return dpsVisitRiseMapper.updateByPrimaryKeySelective(dpsVisitRise);
    }
}
